﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.ObjectModel;
using DeepTime.Shared.Entities;
using DeepTime.DL.DataSources;
using DeepTime.DL.DataModels;
using Microsoft.WindowsAzure.StorageClient;

namespace DeepTime.Web.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Collages
    {
        [OperationContract]
        public void Create(ObservableCollection<UploadedImageCollageMetaDataEntity> UploadedImages)
        {
            //
            // Set the partition key & row keys then persist
            //
            UploadedImageCollageMetaDataDataSource uploadedImageCollageMetaDataDataSource = new UploadedImageCollageMetaDataDataSource();
            string PartitionKey = System.Guid.NewGuid().ToString();

            foreach (UploadedImageCollageMetaDataEntity item in UploadedImages)
            {
                item.PartitionKey = PartitionKey;
                item.RowKey = System.Guid.NewGuid().ToString();

                uploadedImageCollageMetaDataDataSource.Insert(new UploadedImageCollageMetaDataDataModel(item));
            }

            //
            // Add to queue storgate for processing
            //
            CollageImageProcessorQueueDataSource collageImageProcessorQueueDataSource = new CollageImageProcessorQueueDataSource();
            collageImageProcessorQueueDataSource.Insert(new CloudQueueMessage(PartitionKey));
        }
    }
}
